<?php

class Route
{
    public $controller;
    public $action;
    public $params;
    
    public function __construct($controller = "", $action = "", $params = array())
    {
        if (empty($controller))
        {
            $this->loadRoute();
        }
        else
        {
            $this->controller = $controller;
            $this->action = (!empty(trim($action)))?$action:'Index';
            $this->params = $params;
        }
            
    }
    
    public function getParams()
    {
        return $this->params;
    }
    
    private function loadRoute()
    {
        if(isset($_SERVER["PATH_INFO"]))
        {            
            $route = explode("/",$_SERVER["PATH_INFO"]);            
            $this->controller = (isset($route[1]))?ucfirst($route[1]):'Index';
            $this->action = (isset($route[2]))?ucfirst($route[2]):'Index';
            $this->action = (!empty(trim($this->action)))?$this->action:'Index';
            
            for($i=3; $i < count($route); $i=$i+2)
            {
                if (isset($route[$i]) && isset($route[$i+1]) )
                {
                    $name = $route[$i];
                    $value = $route[$i+1];
                    $this->params[$name] = $value;
                }             
            }          
        }
        else
        {
            $this->params = array();
            $this->controller = "Index";
            $this->action = "Index";
        }
    }
    
    public function loadRouteFromUrlString($urlString = "")
    {
        if(!empty($urlString))
        {            
            $route = explode("/",$urlString);            
            $this->controller = (isset($route[1]))?ucfirst($route[1]):'Index';
            $this->action = (isset($route[2]))?ucfirst($route[2]):'Index';
            $this->action = (!empty(trim($this->action)))?$this->action:'Index';
            
            for($i=3; $i < count($route); $i=$i+2)
            {
                if (isset($route[$i]) && isset($route[$i+1]) )
                {
                    $name = $route[$i];
                    $value = $route[$i+1];
                    $this->params[$name] = $value;
                }             
            }          
        }
        else
        {
            $this->params = array();
            $this->controller = "Index";
            $this->action = "Index";
        }
    }
    
    /**
     * Return URL from route
     * @return string
     */
    public function getUrl()
    {
        $url = "/".$this->controller."/".$this->action;
        foreach ($this->params as $name => $value) {
            $url = $url . "/".$name."/".$value;
        }
        return $url;
    }
    
    /**
     * Return URL from route
     * @return string
     */
    public static function Url($controller, $action, $params = array())
    {
        $url =  SERVER_NAME . "/".$controller."/".$action;
        foreach ($params as $name => $value) {
            $url = $url . "/".$name."/".$value;
        }
        return $url;
    }
    
    /**
     * Return list allow URLs 
     * @return string
     */
    public static function AllowUrls()
    {
        $listUrls = unserialize(ALLOW_URLS);
        $listRoutes = array();
        foreach ($listUrls as $item)
        {
            $objRoute = new Route();
            $objRoute->loadRouteFromUrlString($item);
            $listRoutes[]=$objRoute;
        }
        return $listRoutes;
    }
}

